﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GtsServer.Model;
using GtsServer.Common;
using GtsServer.Manage.Controller;

namespace GtsServer.Manage.IISPage.Ajax
{
    /// <summary>
    /// EventChanceAjax1 的摘要说明
    /// </summary>
    public class EventChanceAjax1 : IHttpHandler
    {

        EventChanceController controller = new EventChanceController();
        EventMouldController mouldController = new EventMouldController();
        public void ProcessRequest(HttpContext context)
        {
            string type = string.Empty;
            if (context.Request["action"] != null)
            {
                type = context.Request["action"];

                switch (type)
                {
                    case "GetList":
                        GetList(context);
                        break;
                    case "Update":
                        Update(context);
                        break;
                    default:
                        break;
                }
            }
        }

        private void Update(HttpContext context)
        {
            int id = Convert.ToInt32(context.Request["id"].ToString());
            int chanceValue = Convert.ToInt32(context.Request["chanceValue"].ToString());
            context.Response.Write(controller.UpdateChanceValue(id, chanceValue));
        }

        private void GetList(HttpContext context)
        {
            int chanceTypeID = Convert.ToInt32(context.Request["typeID"].ToString());
            int eventID = Convert.ToInt32(context.Request["eventID"].ToString());
            List<EventChance> eventChanceList = controller.GetList(chanceTypeID, eventID);

            List<EventChanceMould> mouldList = mouldController.GetList(eventID);

            Dictionary<int, string> dict = new Dictionary<int, string>();
            foreach (var item in mouldList)
                dict[item.ID] = item.Name;

            List<EventChanceModelShow> mouldShowList = new List<EventChanceModelShow>();
            foreach (var item in eventChanceList)
            {
                EventChanceModelShow model = new EventChanceModelShow()
                {
                    ID = item.ID,
                    ChanceTypeID = item.ChanceTypeID,
                    ChanceValue = item.ChanceValue,
                    EventID = item.EventID,
                    EventMouldID = item.EventMouldID,
                    MouldName = ""
                };
                if (dict.ContainsKey(item.EventMouldID.Value))
                    model.MouldName = dict[item.EventMouldID.Value];
                mouldShowList.Add(model);
            }
            context.Response.Write(JsonHelper.ListToJson<EventChanceModelShow>(mouldShowList));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}